<?php
	/**
		 * 获取图片并将图片放到某文件夹下
		 * 如果$pic_x或者$pic_y任意一个等于0,就不调整大小
		 * 可以选择是否使用缓存
		 * 可以选择是否删除源文件
		 * 可以选择从第ST个结果开始返回,默认从第一个结果开始返回
		 *
		 * @param string $dir
		 * @param array $db_settings
		 * @param int pic_nums
		 * @param intpic_x
		 * @param int pic_y
		 * @param bool make_pic_hased
		 * @param bool $with_orgpic_del
		 * @param int $st
		 * @return string  array
		 */
	function fetch_pic_to_dir($dir,$db_settings,$pic_nums = 10,$pic_x = 0,$pic_y = 0,$make_pic_hased = false,$with_orgpic_del = true,$pic_id = false,$st = 0){
		if (!is_dir($dir)) {
			$result = true;
			$result = mkdir($dir,0777);
			if (!$result) {
				return ;
			}
		}
		global $DB;
		$pic_nums = intval($pic_nums);
		$pic_mysql = new photo_mysql($db_settings['host'],$db_settings['user'],$db_settings['pass'],$db_settings['dbname'],$db_settings['tb_pre']);
		$prefix_tb = $db_settings['tb_pre'];
		if (!$use_thumb) {
			$sql = "SELECT " . $prefix_tb . "photos.photo," . $prefix_tb . "photothumbs.posttime," . $prefix_tb . "photothumbs.photothumbs_id," .$prefix_tb . "photothumbs.cache_id ";
			$sql .= "FROM " . $prefix_tb . "photos LEFT JOIN  " . $prefix_tb . "photothumbs";
			//notice do not del the blank
			$sql .= " ON (" . $prefix_tb . "photos.photo_id = " . $prefix_tb . "photothumbs.photothumbs_id)";
			if (intval($pic_id) != false) {
				$sql .= "WHERE " . $prefix_tb . "photothumbs.photothumbs_id=" . $pic_id . ' ';
			}
			$sql .= "ORDER BY `posttime` DESC LIMIT $st , $pic_nums ";
			$result = $pic_mysql->fetch_assoc($sql);
		}
		else {
			$result = $DB->build_and_exec_query(array(
			'select' => 'photothumbs as photo,*',
			'from' => 'photothumbs',
			'where'	=> "photothumbs_id = " . $value['photothumbs_id'],
			'limit'	=> array($st,$pic_nums)
			));
		}
		//只更新通过ID访问的图片的点击数
		if (intval($pic_id) != false) {
			$hit = $DB->build_and_exec_query(array('select' => 'hits','from' => 'photothumbs','where'	=> "photothumbs_id = " . $pic_id,));
			$hit = $hit['hits'] + 1;
			$DB->do_update(
			'photothumbs',array('hits'=>"$hit"),'photothumbs_id=' . $pic_id
			);
		}
		//从数据库取出图象
		$pic_name = array();
		$org_pic_name = array();
		foreach ($result as $value) {
			//判断是否有CACHE
			if ($make_pic_hased) {
				$nocache = false;
				$row = $DB->build_and_exec_query(array(
				'select' => 'cache_id',
				'from' => 'photothumbs',
				'where'	=> "photothumbs_id = " . $value['photothumbs_id']
				));
				$result = $row['cache_id'];
				if (intval($result) == 0) {
					$nocache = true;
				}
				else {
					$row = $DB->build_and_exec_query(array(
					'select' => 'cache_folder',
					'from' => 'hash_cache',
					'where'	=> "cache_id = " . $result
					));
					if (!file_exists($row['cache_folder'])) {
						$nocache = true;
					}
				}
				if (!$nocache) {
					//如果已经有缓存,就将缓存文件赋值过去(也就是说不用写文件了)
					//echo 'the file ' . $row['cache_folder'] . ' has been cached<br />';
					$filename = $row['cache_folder'];
				}
				else {
					$filename = $dir . $value['posttime'] .'.jpg';
					//echo 'the file ' . $filename . ' has not been cached <br />';
				}
			}
			else {
				$filename = $dir . $value['posttime'] .'.jpg';
			}
			$org_pic_name[] = $filename;
			if (!file_exists($filename)) {
				@$result = touch($filename);
				if (!$result) {
					return false;
				}
				@$fp = fopen($filename,'w');
				if (!$fp) {
					return false;
				}
				fwrite($fp,$value['photo'],strlen($value['photo']));
				fclose($fp);
				//调整图片到合适大小(如果$pic_x或者$pic_y任意一个等于0,就不调整大小)
				if ($pic_x <= 0 || $pic_y <= 0) {
					$pic_name = $org_pic_name;
					//如果不调整,就返回源文件,并且不删除源文件
				}
				else
				{
					$pic_name[] = resize_pic_for_slidshow($pic_x,$pic_y,$filename);
					if ($with_orgpic_del) {
						unlink($filename);
					}
				}
	
				//如果需要生成缓存,将缓存保存下来
				if ($make_pic_hased) {
					$filehash = $value['posttime'];
					$hash_id = time() . mt_rand(0,999);
					global $DB;
					//插入HASH表
					$DB->do_insert( 'hash_cache' , array( 'cache_id'	=> $hash_id,
					'cache_folder'			=> $filename,
					'uid'					=> 0,
					'date'					=> get_date(time()),
					'result'				=> 1
					));
					//更新图片表的HASH ID
					$DB->do_update( 'photothumbs', array( 'cache_id' => $hash_id ),'photothumbs_id=' . $value['photothumbs_id']);
				}
			}
		}
		//如果删除了源文件,就返回调整大小后的文件,如果没删除,就直接返回源文件列表(调整大小后的图片名只是名字后面多了一个thb)
		if ($with_orgpic_del) {
			return $pic_name;
		}
		else {
			return $org_pic_name;
		}
	}
	/**
	 * 获取文件对象
	 *
	 * @param string $file
	 * @param array $db_settings
	 * @param string $font_name
	 * @param int $pic_x
	 * @param int $pic_y
	 * @return obj
	 */
	function get_photo_obj($file,$db_settings,$font_name = 'DGBN_B6.TTF',$pic_x = 75,$pic_y = 75){
		$file_path = stripslashes($file['savepath']);
		$file_dir = dirname($file_path);
		$file_type = $file['filetype'];
		$file_size = $file['filesize'];
		$file_name = $file['filename'];
		$file_client_path = $file['clientpath'];
		$photo = new photo($db_settings,$file_name,$file_type,$file_path,$font_name,$pic_x,$pic_y);
		
		
		
		return $photo;
		
	}
	/**
	 * 保存单个上传图片
	 * $with_erroe_exit 设置是否显示出错信息并且退出,如果是true,就显示出错信息并且退出,否则返回主处理
	 * 该函数返回上传图片的路径
	 *
	 * @param bool $with_erroe_exit
	 * @return string
	 */
	function save_pic($with_error_exit = true,$use_javaapp = false){
		global $DB;
		global $db_settings;
		global $web_settings;
		$jid = $_POST['jid'];
		
		if (!is_numeric($jid)) {
			if ($with_error_exit) {
				show_normal_mesR('无法获取当前任务编号','任务编号错误','图片上传');
			}
			else 
				return;
		}
		
		$pic_info = $_POST['uptp'];
		$pic_info = explode('|',$pic_info,4);
		$pic_title = trim($pic_info[0]);
		$pic_cat = trim($pic_info[1]);
		$pic_des = trim($pic_info[2]);
		//注意这里的TAG是数组
		$pic_tags = explode(',',trim($pic_info[3]));
		//用户ID就是相册ID
		$album_id = $_SESSION['uid'];
		if ($album_id == '') {
			$album_id = $_GET['uid'];
		}
		//处理上传文件
		$photo = get_photo_obj($_POST['FileUp'],$db_settings,'DGBN_B6.TTF',80,80);
		//得到上传文件的物理路径
		$result = $photo->save_pic($db_settings,$pic_cat,$jid,$album_id,'',$pic_title,$pic_des,true,true);
		if ($result < 0) {
			if ($with_error_exit) {
				show_normal_mesR('非法图片<br />您的图片类型为:' . $_POST['FileUp']['filetype'],'非法图片','图片上传');
			}
			else 
				return;
		}
		//获取图片ID
		$pid = $photo->get_pid();
		
		//本来这里可以直接获取图片的相对连接的,但是最好还是从数据库中查询出来保持兼容(这里用的前者,如果发生错误,就换后者)
		$hash_dirname = explode('upload/',$result);
		$hash_dirname = dirname($hash_dirname[1]);
		$latest_pic_url = SITE_URL . 'upload/' . $hash_dirname . '/';
		$tmp_str = str_replace('.jpg','thb.jpg',strtolower($result));
		//获取缩略图文件
		$thbfile = str_replace('.jpeg','thb.jpeg',strtolower($tmp_str));
		//获取缩略图以及原图的URL
		$pic_thb = $latest_pic_url . basename(strtolower($thbfile));
		$pic = $latest_pic_url . basename(strtolower($result));
		$imgurl = $pic;
		//更新标签表以及用户表以及分类表
		$pic_tags = ADD_addslashes($pic_tags);
		foreach ($pic_tags as $k=>$v) {
			if (trim($v) == '') {
				continue;
			}
			//将TAG标签插入到数据库中(分类是一对多关系,不用更新分类了)
			$tid = time() . mt_rand(100,999);
			$DB->do_insert( 'tags_setting', array( 'tid'=> $tid, 'photo_id'=>$pid,'jid' => $jid,'tag_name'=> $v,'uid'=>$album_id));
		}
		return $imgurl;
	}
	/**
		 * 调整图片大小
		 *
		 * @param int $pic_x
		 * @param int $pic_y
		 * @param string $path
		 * @return string
		 */
	function resize_pic_for_slidshow($pic_x,$pic_y,$path){
		if ($pic_x <= 0 || $pic_y <= 0) {
			$pic_x = $pic_y = 75;
		}
		$resizer = new photo_function(intval($pic_x),intval($pic_y),$path);
		$pic_resized = $resizer->resize_and_cut_pic($path,true,false);
		return basename($pic_resized);
	}
	/**
		 * 获取缓存文件夹,1代表绝对路径,0代表相对路径
		 *
		 * @param  string $upload_dir
		 * @return array
		 */
	function pic_hash_dir($upload_dir){
		$dirname[0] = get_dirname_by_time(time());
		$dirname[1] = ROOT_PATH . $upload_dir . '/' . $dirname[0] . '/';
		return $dirname;
	}
	/**
	 * 检测用户名,合法就返回原来的用户名,不合法就返回小于0的值
	 *
	 * @param string $org_user_name
	 * @return mixed
	 */
	function check_user($org_user_name){
		$cmp_value = '^[a-zA-Z0-9_]{4,30}$';
		if (!ereg($cmp_value,$org_user_name)) {
			return -1;
		}
		//长度不符合
		if (strlen($org_user_name) > 30 || strlen($org_user_name) < 4) {
			return  -2;
		}
		return $org_user_name;
	}
	/**
		 * 将搜索字符串中的关键词分割
		 *
		 * @param 搜索字符串 $search_str
		 * @return 关键词数组
		 */
	function get_searching_strs($search_str){
		$search_strs = array();
		$sadkf= explode('\"',$search_str);
		$count = count($sadkf);
		//字符串内没有冒号
		if ($count == 1) {
			$count = 0;
		}
		//冒号不匹配(去掉全部冒号,用空格做分隔符号)
		if (($count + 1) % 2 != 0) {
			$sadkf= str_replace('\"','',$search_str);
			$sadkf= preg_split('/ /',$sadkf,-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
			$count = count($sadkf);
			for ($i = 0; $i < $count; $i++){
				$search_strs[] = $sadkf[$i];
			}
			return $search_strs;
		}
		//冒号匹配(冒号内字符串失效,冒号外空格做匹配符号,模式匹配:冒号后面所有字符串,直到下一个冒号)
		else {
			//去掉转义
			$search_str = STRIP_stripslashes($search_str);
			$search_str_len = strlen($search_str);
			$test1 = array();
			$quote_num = 0;
			$test2 = 0;
			$byte_in_quote = 0;
			for ($i = 0; $i < $search_str_len; $i++){
				if ($search_str[$i] == '"') {
					//记录目前到达引号的字节
					$byte_in_quote = $i;
					//记录当前引号数
					$quote_num ++;
					if (($quote_num  == (($quote_num >> 1) << 1) && $quote_num == 2) || $quote_num > 2) {
						$test2 += 1;
						//判断是否是被引起来的
						//是
						if ($quote_num  === (($quote_num >> 1) << 1)) {
							$search_strs[] = $test1[$test2 - 1];
						}
						//否
						else {
							//按照空格进行分割
							$tmp_str = preg_split('/ /',$test1[$test2 - 1],-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
							$search_strs = give_value_of_one_array_to_another($search_strs,$tmp_str);
						}
					}
				}
				else {
					$test1[$test2] .= $search_str[$i];
				}
			}
			//最后一个子串不是用引号包含
			if ($byte_in_quote != $i -1) {
				//按照空格进行分割
				$tmp_str = preg_split('/ /',$test1[$test2],-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
				$search_strs = give_value_of_one_array_to_another($search_strs,$tmp_str);
			}
			return $search_strs;
		}
	}
	/**
		 * 最大搜索间隔时间
		 *
		 * @param string $action
		 * @param string $key
		 * @param int $maxtime
		 */
	function set_max_interval($action = '搜索',$key = 'action',$maxtime = '300'){
		if ((intval($_SESSION[$key]) + $maxtime) > time()) {
			show_normal_mesR('两次'. $action . '时间间隔不能超过' . $maxtime . '秒',$action . '间隔过短',$action);
		}
		else{ //记录当前搜索时间
			$_SESSION[$key] = time();
		}
	}
	//检测COOKIE
	function check_cookie(){
		global $DB;
		if (isset($_COOKIE['user_name']) && $_COOKIE['user_name'] != '') {
			$cookie_user_name = check_user($_COOKIE['user_name']);
			if ($cookie_user_name < 0) {
				//COOKIE名不合法
				return -1;
			}
			else {
				//读用户信息表
				$row = $DB->build_and_exec_query(array(
				'select' => '*',
				'from' => 'member_info',
				'where'	=> "name = '" . $cookie_user_name . "'",
				));
				$num_rows = $DB->get_num_rows();
				if ($num_rows != 1) {
					//不存在该用户
					return -2;
				}
				$user_infos = array();
				$org_user_infos = $row;
				$user_infos = array();
				$user_infos['user_name'] = $org_user_infos['name'];
				$user_infos['user_pass'] = $org_user_infos['pwd'];
				$user_infos['user_email'] = $org_user_infos['email'];
				$user_infos['user_gander'] = $org_user_infos['gander'];
				$user_infos['reg_time'] = $org_user_infos['reg_time'];
				$user_infos['uid'] = $org_user_infos['id'];
				$user_infos['gid'] = $org_user_infos['gid'];
				$user_infos['blog_album_describe'] = $org_user_infos['blog_album_describe'];
				$user_infos['photo_album_name'] = $org_user_infos['photo_album_name'];
	
				//检测密码是否符合
				if (sha1($user_infos['user_pass']) !== $_COOKIE['user_sha1_pass']) {
					return -3;
				}
				//注册变量
				regist_user_info($user_infos,true);
				return true;
			}
		}
	}
	/**
		 * 获取图片分类
		 *
		 * @param string $field
		 * @return array
		 */
	function get_category($field) {
		global $DB;
		$DB->build_query( array(  'select' => '*',
		'from'   => $field,
		'order'  => 'id DESC'
		)   );
		$DB->exec_query();
		while( $row = $DB->fetch_row() )
		$category[] = array('name'=>$row['sort_name'],'value'=>$row['id']);
		return $category;
	}
	/**
	 * 获取工作列表
	 *
	 * @param unknown_type $field
	 * @param unknown_type $listnum
	 * @param unknown_type $st
	 * @param unknown_type $where
	 * @param unknown_type $order
	 * @return unknown
	 */
	function get_jobs_of_uid($field,$listnum,$st,$where,$order = 'job_id DESC'){
		global $DB;
		if (intval($jid) <= 0) {
			$DB->build_query( array(  'select' => '*',
			'from'   => $field,
			'where'  => $where ,
			'limit'	 => array($st,$listnum),
			'order'  => $order
			)   );
		}
		else {
			$DB->build_query( array(  'select' => '*',
			'from'   => $field,
			'where'  => $where,
			'limit'	 => array($st,$listnum),
			'order'  => $order
			)   );
		}
		$DB->exec_query();
		while( $row = $DB->fetch_row() )
		$jobs[] = $row;
		return $jobs;
	}
	/**
	 * 获取某个列表的总个数
	 *
	 * @param string $field
	 * @param string $where
	 * @return int
	 */
	function get_count_of_field($field,$where){
		global $DB;
		$result = $DB->build_and_exec_query( array(  
			'select' => 'COUNT(*) as max',
		    'from'   => $field,
		    'where'   => $where,
		)   );
		return $result['max'];
	}
	/**
	 * 获取缓存文件的绝对URL
	 *
	 * @param unknown_type $org_path
	 * @param unknown_type $upload_path
	 * @return array
	 */
	function get_pic_url($org_path,$upload_path){
		//获取缓存文件的文件夹(基于UPLOAD文件夹的)
		$hash_dirname = explode($upload_path . '/',$org_path);
		$hash_dirname = dirname($hash_dirname[1]);
		$org_file = basename($org_path);
		$latest_pic_url = SITE_URL . 'upload/' . $hash_dirname . '/';
		$tmp_str = str_replace('.jpg','thb.jpg',strtolower($org_path));
		//获取缩略图文件
		$thbfile = str_replace('.jpeg','thb.jpeg',strtolower($tmp_str));
		$pic_url['thb'] = $latest_pic_url . basename(strtolower($thbfile));
		$pic_url['org'] = $latest_pic_url . strtolower($org_file);
		return $pic_url;
	}
	/**
	 * 将密码发送到用户邮箱
	 *
	 * @param string $user_name
	 * @param string $pass
	 * @param string $email
	 * @param array $web_settings
	 * @param string $site_url
	 * @return bool
	 */
	function send_pass($user_name,$pass,$email,$web_settings){
		@require_once('Mail.php');
		$conf['mail'] = array(
		'host'     => $web_settings['smtp'],         //smtp服务器地址，可以用ip地址或者域名
		'auth'     => true,                          //true表示smtp服务器需要验证，false代码不需要
		'username' => $web_settings['smtp_name'],    //用户名
		'password' => $web_settings['smtp_pass']     //密码
		);
		/*** 使用$headers数组，可以定义邮件头的内容，比如使用$headers['Reply-To']可以定义回复地址
		* 通过这种方式，可以很方便的定制待发送邮件的邮件头 ***/
		$headers['From']    = 'fakesky@163.com';              //发信地址
		$headers['To']      = $email;              //收信地址
		$headers['Subject'] = show_in_gb('[' . $web_settings['site_name'] . ']密码找回:',SYS_CODE);          //邮件标题
		$headers['Content-type'] = 'text/html; charset="GB2312"';
		$mail_object = &Mail::factory('smtp', $conf['mail']);
		$body = '
			<html>
				<body>
					尊贵的用户:' . $user_name . '<br>您的密码为:' . $pass . '<br>请尽快<a href="'.SITE_URL.'"><b>登陆</b></a>修改密码
				</body>
			</html>';
		$body = show_in_gb($body,SYS_CODE);
		$mail_res = $mail_object->send($headers['To'], $headers, $body);        //发送
		if ($mail_res !== true) {
			return false;
		}
		else {
			return true;
		}
	}
	/**
	 * 将编码转换为指定编码
	 * $str为需要转换的字符串
	 * $from_encode为字符串的编码
	 * $to_encode为需要转换的编码
	 * 
	 * 
	 * @param string $str
	 * @param string $from_encode
	 * @param string $to_encode
	 * @return string
	 */
	function show_in_gb($str,$from_encode = 'UTF-8',$to_encode = 'GB2312'){
		return mb_convert_encoding($str,$to_encode,$from_encode);
	}
	/**
	 * 自动转化编码
	 *
	 * @param sting $v
	 * @return string
	 */
	function auto_decoding($v){
		$encoding = mb_detect_encoding($v,"GB2312,BIG5,CP936,UTF-8");
		$v = show_in_gb(STRIP_stripslashes($v),$encoding,SYS_CODE);
		return $v;
	}
	//获取分类下点击率最高的一张图的全部信息
	function get_hot_pic($cid,$uid=false){
		global $DB,$db_settings;
		if (!$uid) {
			$row = $DB->build_and_exec_query(array(
				'select' => '*',
				'from' => 'photothumbs',
				'where'	=> "sort_id = " . $cid,
				'order'  => 'hits DESC',
				'limit'  => array( 0, 1 )
			) );
		}
		else {
			$row = $DB->build_and_exec_query(array(
				'select' => '*',
				'from' => 'photothumbs',
				'where'	=> "sort_id = " . $cid . ' AND album_id=' . $uid,
				'order'  => 'hits DESC',
				'limit'  => array( 0, 1 )
			) );
		}
		return $row;
	}
	//分块
	function devide_itemlist($itemlist,$divnum = 5){
		$itemlist1 = array();
		$itemlist2 = array();
		$itemlist3 = array();
		$i = 0;
		foreach ($itemlist as $k=>$v) {
			if ($i < $divnum) {
				$itemlist1[] = $itemlist[$k];
			}
			if ($i > ($divnum - 1) && $i < (2 * $divnum)) {
				$itemlist2[] = $itemlist[$k];
			}
			if ($i > (2 * $divnum - 1)) {
				$itemlist3[] = $itemlist[$k];
			}
			$i ++;
		}
		unset($i);
		$itemlists = array($itemlist1,$itemlist2,$itemlist3);
		return $itemlists;
	}
	/**
	 * 获取没有图片的任务ID
	 *
	 * @param int $uid
	 */
	function get_empty_jobids($uid){
		global $DB,$db_settings;
		$prefix_tb = $db_settings['tb_pre'];
		$sql = "SELECT j.job_id AS jjid,p.job_id AS pjid ";
		$sql .= "FROM " . $prefix_tb . "job  AS j LEFT JOIN  " . $prefix_tb . "photothumbs  AS p ";
		//notice do not del the blank
		$sql .= "ON j.job_id=p.job_id ";
		$sql .= "WHERE j.authorid = " . $uid . ' ';
		$DB->cur_query = $sql;
		$DB->exec_query();
		$empty_jobs = array();
		while ($row = $DB->fetch_row()) {
			if ($row['pjid'] == '') {
				$empty_jobs[] = $row['jjid'];
			}
		}
		return $empty_jobs;
	}
	/**
	 * 获取没有图片的任务ID
	 *
	 * @param int $uid
	 */
	function get_empty_picids($uid){
		global $DB,$db_settings;
		$prefix_tb = $db_settings['tb_pre'];
		$sql = "SELECT j.job_id AS jjid,p.job_id AS pjid,p.photothumbs_id AS pid ";
		$sql .= "FROM " . $prefix_tb . "job  AS j RIGHT JOIN  " . $prefix_tb . "photothumbs  AS p ";
		//notice do not del the blank
		$sql .= "ON j.job_id=p.job_id ";
		$sql .= "WHERE p.album_id = " . $uid . ' ';
		$DB->cur_query = $sql;
		$DB->exec_query();
		$empty_jobs = array();
		while ($row = $DB->fetch_row()) {
			if ($row['jjid'] == '') {
				$empty_jobs[] = $row['pid'];
			}
		}
		return $empty_jobs;
	}
	/**
	 * 获取权限列表
	 *
	 * @param int $gid
	 * @return array
	 */
	function get_rights_table($gid){
		global $DB;
		if (!is_numeric($gid)) {
			return false;
		}
		$rights = $DB->build_and_exec_query(array(
			'select' => '*',
			'from' => 'admin_info',
			'where' => 'gid=' . $gid,
		));
		$rights_table['can_admin'] = $rights['can_admin'];
		$rights_table['can_user'] = $rights['can_user'];
		$rights_table['can_category'] = $rights['can_category'];
		$rights_table['can_setting'] = $rights['can_setting'];
		return $rights_table;
	}
	function get_num_jobs($num,$uid,$DB,$db_settings,$all_jobs = false){
		$defaultlistnum = $num;
		$job_list = array();
		$DB->build_query(array(
			'select' 	=> 'job_id,article_title',
			'from'		=> 'job',
			'where'		=> 'authorid=' . $uid,
			'order'		=> 'posttime ASC',
		));
		$qid = $DB->exec_query();
		$jids = array();
		$i = 0;
		while ($row = $DB->fetch_row($qid)) {
			//判断该JOB下面是否有图片
			$count = $DB->build_and_exec_query(array(
				'select' => 'count(*) as count',
				'from' => 'photothumbs',
				'where' => 'job_id=' . $row['job_id'] ,
			));
			$count = $count['count'];
			if ($count == 0) {
				continue;
			}
			//判断是否够显示了
			if (!$all_jobs) {
				if ($i >= $defaultlistnum) {
					break;
				}
			}
			$jids[] = $row['job_id'];
			$title_list[$i]['article_title'] = STRIP_stripslashes(trim($row['article_title']));
			$i++;
		}
		unset($i);
		//根据JID获取到该JOB下的最新的一张图(或者点击率)
		$dirname = pic_hash_dir('upload');
		$hash_dirname = $dirname[0];
		$dirname = $dirname[1];
		foreach ($jids as $k=>$v) {
			$row = $DB->build_and_exec_query(array('select' => '*','from'=>'photothumbs','where' => 'job_id=' . $v,'order' => 'posttime DESC','limit' => array(0,1)));
			$job_list[$k] = $row;
			$file = fetch_pic_to_dir($dirname,$db_settings,1,80,80,true,false,$row['photothumbs_id']);
			$file = $file[0];
			$links = get_pic_url($file,'upload');
			//获取JOB连接
			$job_list[$k]['link'] = SITE_URL . "photolist.php?action=jid&jid=" . $v;//$links['org'];
			$job_list[$k]['thb'] = $links['thb'];
			$job_list[$k]['article_title'] = STRIP_stripslashes(trim($title_list[$k]['article_title']));
		}
		return $job_list;
	}
	function get_num_jobs_custom($num,$uid,$DB,$db_settings,$where,$order){
		$defaultlistnum = $num;
		$job_list = array();
		$DB->build_query(array(
			'select' 	=> 'job_id,article_title',
			'from'		=> 'job',
			'where'		=> $where,
			'order'		=> $order,
		));
		$qid = $DB->exec_query();
		$jids = array();
		$i = 0;
		while ($row = $DB->fetch_row($qid)) {
			//判断该JOB下面是否有图片
			$count = $DB->build_and_exec_query(array(
				'select' => 'count(*) as count',
				'from' => 'photothumbs',
				'where' => 'job_id=' . $row['job_id'] ,
			));
			$count = $count['count'];
			if ($count == 0) {
				continue;
			}
			//判断是否够显示了
			if ($i >= $defaultlistnum) {
				break;
			}
			$jids[] = $row['job_id'];
			$title_list[$i]['article_title'] = STRIP_stripslashes(trim($row['article_title']));
			$i++;
		}
		unset($i);
		//根据JID获取到该JOB下的最新的一张图(或者点击率)
		$dirname = pic_hash_dir('upload');
		$hash_dirname = $dirname[0];
		$dirname = $dirname[1];
		foreach ($jids as $k=>$v) {
			$row = $DB->build_and_exec_query(array('select' => '*','from'=>'photothumbs','where' => 'job_id=' . $v,'order' => 'posttime DESC','limit' => array(0,1)));
			$job_list[$k] = $row;
			$file = fetch_pic_to_dir($dirname,$db_settings,1,80,80,true,false,$row['photothumbs_id']);
			$file = $file[0];
			$links = get_pic_url($file,'upload');
			//获取JOB连接
			$job_list[$k]['link'] = SITE_URL . "photolist.php?action=jid&jid=" . $v;//$links['org'];
			$job_list[$k]['thb'] = $links['thb'];
			$job_list[$k]['article_title'] = STRIP_stripslashes(trim($title_list[$k]['article_title']));
		}
		return $job_list;
	}
	function get_follow_cids($cats,$num,$uid,$cid,$jid,$tag_name,$DB,$db_settings,$return_list=false){
		$result = array();
		//获取分类下的文件并且返回文件名
		$cat_list = array();
		//获取分类
		$i = 0;
		foreach ($cats as $key=>$value) {
			//获取CACHE文件
			$result[$key] = get_hot_pic($value['value'],$uid);
			$pid = $result[$key]['photothumbs_id'];
			if (intval($pid) == 0) {
				//也就是说该类型还没文件
				unset($cats[$key]);
				continue;
			}
			$dirname = ROOT_PATH . 'upload/' . get_dirname_by_time(time()) . '/';
			//注意,这里如果已经有缓存了,就会直接返回缓存路径
			$filename = fetch_pic_to_dir($dirname,$db_settings,1,80,80,true,false,$pid);
			//打印分类名称以及文件名称
			$pic_urls = get_pic_url($filename[0],'upload');
			//初始化类别数组
			$cat_list[$i]['org_url'] = $pic_urls['org'];
			$cat_list[$i]['thb_url'] = $pic_urls['thb'];
			$cat_list[$i]['sort_name'] = STRIP_stripslashes(trim($value['name']));
			//注意,超级连接没有记忆功能,有待改进
			$cat_list[$i]['cat_link'] = SITE_URL . 'user.php?uid=' . $uid . '&cid=' . $value['value'];
			$cat_list[$i]['value'] = $value['value'];
			$i++;
		}
		unset($i);
		//如果数量大于1,并且设置为返回列表,就返回当前列表
		if ($return_list) {
			if ($num > 0) {
				$countc = count($cat_list);
				for ($i = 0;$i < $num;$i++){
					if ($i == $countc) {
						break;
					}
					$catlist_return[$i] = $cat_list[$i];
					//重新修改为查看连接(根据用户名显示用户某个分类下的图片的连接)
					$catlist_return[$i]['cat_link'] = SITE_URL . 'photolist.php?uid=' . $uid . '&action=cat&cid=' . $cat_list[$i]['value'];
				}
				return $catlist_return;
			}
		}
		/**
		 * this is the change point to set pics to show
		 */
		//如果没有CID,默认从第二个有图片的CID开始显示
		if ($cid == '') {
			$cid = $cat_list[1]['value'];			//the org is $cid = $cat_list[0]['value'];
		}
		
		//获取比当前ID小的CID和比当前CID大的CID各一
		$count_cat = count($cat_list);
		foreach ($cat_list as $k=>$v) {
			if ($v['value'] == $cid) {
				$temp_pointer = $k - 1;
				while (!isset($cat_list[($temp_pointer)]) && $temp_pointer > 0) {
					$temp_pointer--;
				}
				$pre_cid = $cat_list[$temp_pointer];
				$temp_pointer = $k + 1;
				while (!isset($cat_list[$temp_pointer]) && $temp_pointer < $count_cat) {
					$temp_pointer++;
				}
				$next_cid = $cat_list[$temp_pointer];
				$current_cid = $cat_list[$k];
			}
		}
		$cats = array($next_cid,$current_cid,$pre_cid);
		foreach ($cats as $k=>$v) {
			if ($v == '') {
				unset($cats[$k]);
			}
		}
		$count_cat = count($cats);
		if ($count_cat == 0) {
			$cat_html = '
					<div class="threecolbox_lastphoto">&nbsp;</div>
					<div class="threecolbox_lastphoto">&nbsp;</div>
					<div class="threecolbox_lastphoto">&nbsp;</div>
			';
			$pre_link = $next_link = '#';
		}else if ($count_cat == 1) {
			$cat_html = '
					<div class="threecolbox_lastphoto">&nbsp;</div>
					<div class="threecolbox_lastphoto">
						<a href="' . $cats[1]['cat_link'] . '" title="' . $cats[1]['sort_name'] . '">
							<img src="' . $cats[1]['thb_url'] . '" alt="' . $cats[1]['sort_name'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">&nbsp;</div>
			';
			$pre_link = $next_link = '#';
		}else if ($count_cat == 2) {
			//如果CAT[0]没有,就将CAT2复制过去
			if (!isset($cats[0])) {
				$cats[0] = $cats[2];
			}
			$cat_html = '
					<div class="threecolbox_lastphoto">
						<a href="' . $cats[0]['cat_link'] . '" title="' . $cats[0]['sort_name'] . '">
							<img src="' . $cats[0]['thb_url'] . '" alt="' . $cats[0]['sort_name'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">
						<a href="' . $cats[1]['cat_link'] . '" title="' . $cats[1]['sort_name'] . '">
							<img src="' . $cats[1]['thb_url'] . '" alt="' . $cats[1]['sort_name'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">&nbsp;</div>
			';
			$pre_link = $cats[0]['cat_link'];
			$next_link = '#';
		}else {
			$cat_html = '
					<div class="threecolbox_lastphoto">
						<a href="' . $cats[0]['cat_link'] . '" title="' . $cats[0]['sort_name'] . '">
							<img src="' . $cats[0]['thb_url'] . '" alt="' . $cats[0]['sort_name'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">
						<a href="' . $cats[1]['cat_link'] . '" title="' . $cats[1]['sort_name'] . '">
							<img src="' . $cats[1]['thb_url'] . '" alt="' . $cats[1]['sort_name'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">
						<a href="' . $cats[2]['cat_link'] . '" title="' . $cats[2]['sort_name'] . '">
							<img src="' . $cats[2]['thb_url'] . '" alt="' . $cats[2]['sort_name'] . '" width="49" height="41" border="0" />
						</a>
					</div>
			';
			$pre_link = $cats[0]['cat_link'];
			$next_link = $cats[2]['cat_link'];
		}
		$page_html = '
			<div class="twocolcont_next">
				<div id="tools" class="twocolboxl_next">
					<div align="left">
						<a href="'. $pre_link . '">
							<img src="'. SITE_URL . 'images/photobotton_07.gif" alt="" width="14" height="15" border="0" />
						</a>
					</div>
				</div>
				<div id="tools" class="twocolboxr_next">
					<a href="'. $next_link . '">
						<img src="'. SITE_URL . 'images/photobotton_10.gif" alt="" width="14" height="15" border="0" />
					</a>
				</div>
			</div>
		';
		$cat_html .= $page_html;
		return $cat_html;
	}
	function get_follow_tags($num,$uid,$cid,$jid,$tag_name,$DB,$db_settings,$return_list=false){
		//提取标签列表
		//获取当前用户图片的所有标签以及各个标签下的某一图片ID(注意标签有可能有重名,数据库设计上的一点小失误)
		$DB->build_query(array(
			'select' 	=>  '*',
			'from'		=>  'tags_setting',
			'where'		=>  'uid=' . $uid . ' AND photo_id != 0',		//只取图片标签,不要JOB标签
		));
		$DB->cur_query .= ' GROUP BY `tag_name` ORDER BY tid DESC';
		$DB->exec_query();
		//获取内容
		$i = 0;
		$tag_pic_list = array();
		
		while ($row = $DB->fetch_row()) {
			$taglist[$i]['tagname'] = STRIP_stripslashes(trim($row['tag_name']));
			//注意,超级连接没有记忆功能,有待改进
			$taglist[$i]['taglink'] = SITE_URL . 'user.php?uid=' . $uid . '&tagname=' . urlencode($row['tag_name']);
			$taglist[$i]['pid'] = $row['photo_id'];
			$taglist[$i]['tid'] = $row['tid'];
			//获取图片数组
			$tag_pic_list[$i] = $taglist[$i];
			$i++;
		}
		unset($i);
		//对图片列表单独处理图片获取
		foreach ($tag_pic_list as $k=>$v) {
			$dirname = pic_hash_dir('upload');
			$hash_dirname = $dirname[0];
			$dirname = $dirname[1];
			$filename = fetch_pic_to_dir($dirname,$db_settings,1,49,41,true,false,$v['pid']);
			$pic_urls = get_pic_url($filename[0],'upload');
			$tag_pic_list[$k]['tagthburl'] = $pic_urls['thb'];
		}
		//如果数量大于1,并且设置为返回列表,就返回当前列表
		if ($return_list) {
			if ($num > 0) {
				$countt = count($tag_pic_list);
				for ($i = 0;$i < $num;$i++){
					if ($i == $countt) {
						break;
					}
					$catlist_return[$i] = $tag_pic_list[$i];
					//重新修改为查看连接(根据用户名显示用户某个分类下的图片的连接)
					$catlist_return[$i]['taglink'] = SITE_URL . 'photolist.php?uid=' . $uid . '&action=tag&tagname=' . urlencode($tag_pic_list[$i]['tagname']);
				}
				return $catlist_return;
			}
		}
		/**
		 * this is the change point to set pics to show
		 */
		//如果没有tagname,默认从第二个有tagname的tag开始显示
		if ($tag_name == '') {
			$tag_name = $tag_pic_list[1]['tagname'];		//the org is $tag_named = $tag_pic_list[0]['tag_name'];
		}
		
		//获取比当前TID小的TID和比当前TID大的TID各一
		$count_tag = count($tag_pic_list);
		foreach ($tag_pic_list as $k=>$v) {
			if ($tag_name == $v['tagname']) {
				$temp_pointer = $k - 1;
				while (!isset($tag_pic_list[($temp_pointer)]) && $temp_pointer > 0) {
					$temp_pointer--;
				}
				$pre_tid = $tag_pic_list[$temp_pointer];
				$temp_pointer = $k + 1;
				while (!isset($tag_pic_list[$temp_pointer]) && $temp_pointer < $count_cat) {
					$temp_pointer++;
				}
				$next_tid = $tag_pic_list[$temp_pointer];
				$current_tid = $tag_pic_list[$k];
			}
		}
		$tags = array($next_tid,$current_tid,$pre_tid);
		
		
		foreach ($tags as $k=>$v) {
			if ($v == '') {
				unset($tags[$k]);
			}
		}
		$count_tags = count($tags);
		if ($count_tags == 0) {
			$tags_html = '
					<div class="threecolbox_lastphoto">&nbsp;</div>
					<div class="threecolbox_lastphoto">&nbsp;</div>
					<div class="threecolbox_lastphoto">&nbsp;</div>
			';
			$pre_link = $next_link = '#';
		}else if ($count_tags == 1) {
			$tags_html = '
					<div class="threecolbox_lastphoto">&nbsp;</div>
					<div class="threecolbox_lastphoto">
						<a href="' . $tags[1]['taglink'] . '" title="' . $tags[1]['tagname'] . '">
							<img src="' . $tags[1]['tagthburl'] . '" alt="' . $tags[1]['tagname'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">&nbsp;</div>
			';
			$pre_link = $next_link = '#';
		}
		else if ($count_tags == 2) {
			//如果CAT[0]没有,就将CAT2复制过去
			if (!isset($tags[0])) {
				$tags[0] = $tags[2];
			}
			$tags_html = '
					<div class="threecolbox_lastphoto">
						<a href="' . $tags[0]['taglink'] . '" title="' . $tags[0]['tagname'] . '">
							<img src="' . $tags[0]['tagthburl'] . '" alt="' . $tags[0]['tagname'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">
						<a href="' . $tags[1]['taglink'] . '" title="' . $tags[1]['tagname'] . '">
							<img src="' . $tags[1]['tagthburl'] . '" alt="' . $tags[1]['tagname'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">&nbsp;</div>
			';
			$pre_link = $tags[0]['taglink'];
			$next_link = '#';
		}
		else {
			$tags_html = '
					<div class="threecolbox_lastphoto">
						<a href="' . $tags[0]['taglink'] . '" title="' . $tags[0]['tagname'] . '">
							<img src="' . $tags[0]['tagthburl'] . '" alt="' . $tags[0]['tagname'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">
						<a href="' . $tags[1]['taglink'] . '" title="' . $tags[1]['tagname'] . '">
							<img src="' . $tags[1]['tagthburl'] . '" alt="' . $tags[1]['tagname'] . '" width="49" height="41" border="0" />
						</a>
					</div>
					<div class="threecolbox_lastphoto">
						<a href="' . $tags[2]['taglink'] . '" title="' . $tags[2]['tagname'] . '">
							<img src="' . $tags[2]['tagthburl'] . '" alt="' . $tags[2]['tagname'] . '" width="49" height="41" border="0" />
						</a>
					</div>
			';
			$pre_link = $tags[0]['taglink'];
			$next_link = $tags[2]['taglink'];
		}
		$page_html = '
			<div class="twocolcont_next">
				<div id="tools" class="twocolboxl_next">
					<div align="left">
						<a href="'. $pre_link . '">
							<img src="'. SITE_URL . 'images/photobotton_07.gif" alt="" width="14" height="15" border="0" />
						</a>
					</div>
				</div>
				<div id="tools" class="twocolboxr_next">
					<a href="'. $next_link . '">
						<img src="'. SITE_URL . 'images/photobotton_10.gif" alt="" width="14" height="15" border="0" />
					</a>
				</div>
			</div>
		';
		$tags_html .= $page_html;
		return $tags_html;
	}
	
	/**
	 * 获取工作列表
	 *
	 * @param unknown_type $field
	 * @param unknown_type $listnum
	 * @param unknown_type $st
	 * @param unknown_type $where
	 * @param unknown_type $order
	 * @return unknown
	 */
	function custom_query($select,$field,$where,$order = 'job_id DESC'){
		global $DB;
		$DB->build_query( array(  'select' => $select,
		'from'   => $field,
		'where'  => $where ,
		'order'  => $order
		)   );
		$DB->exec_query();
		while( $row = $DB->fetch_row() )
		$jobs[] = $row;
		return $jobs;
	}
	
?>